import random
from tk_test import *


def newint():
    sign = ['+','-','x','/']
    num = random.randint(0,2)
    n1 = random.randint(1,20)
    n2 = random.randint(1,20)
    if num == 0:
        ans = n1 + n2
    elif num == 1:
        n1,n2 = max(n1,n2),min(n1,n2)
        ans = n1 - n2
    elif num == 2:
        ans = n1 * n2
    elif num == 3:
        while n1 % n2 != 0:
            n1 = random.randint(1,20)
            n2 = random.randint(1,20)
            n1,n2 = max(n1,n2),min(n1,n2)
        ans = n1 / n2

    # 将问题描述拼接成字符串
    res1 = " ".join((str(n1), str(sign[num]), str(n2),'='))

    # 返回 问题和标准答案
    return int(ans), res1
    

def main2():
    # 用 ans, res1 接收 newint方法返回的问题和答案
    ans, res1 = newint()

    # 实例化答题界面
    app = App()
    
    app.text1.insert(END, res1)  # 插入问题

    app.com = str(ans)  # 标准答案

    # 显示答题界面
    app.run()

    # 调用 return_res 返回答题比较的结果
    result = app.return_res()
    if  result :
        return result
    else:
        with open('算数错题本.txt', "a") as new_file:
            new_file.write(res1 + '\n')
            
            new_file.close()
        return result
    
if __name__ == "__main__":
    main2()
    
